package com.xxxy.jsq.action.level;

import com.xxxy.jsq.dao.LevelDao;
import com.xxxy.jsq.model.Level;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class UpdateLevelServlet
 */
@WebServlet("/updateLevelServlet")
public class updateLevelServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public updateLevelServlet() {
        super();
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        Level model = new Level();
        LevelDao dao = new LevelDao();

        // 获取页面参数
        String levelName = request.getParameter("levelName");
        String levelFlag = request.getParameter("levelFlag");
        String levelPid = request.getParameter("levelOrder");

        String cid = request.getParameter("levelId");




        if (cid == null) {
            PrintWriter out = response.getWriter();
            out.print("Err");
            out.flush();
            out.close();
            return;
        }


        if (levelName != null && !levelName.isEmpty()) {
            model.setLevelName(levelName);
        }
        if (levelFlag != null && !levelFlag.isEmpty()) {
            model.setLevelFlag(levelFlag);
        }
        if (levelPid != null && !levelPid.isEmpty()) {
            model.setLevelId(Long.valueOf(levelPid));
        }
        if (levelPid != null && !levelPid.isEmpty()) {
            model.setLevelOrder(Integer.valueOf(levelPid));
        }

        model.setLevelId(Long.valueOf(cid));

        // 调用 DAO 更新数据
        String result = dao.update(model);

        // 返回结果给前端
        PrintWriter out = response.getWriter();
        out.print(result);
        out.flush();
        out.close();
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
